package com.kaigejava.demo.business.service.grab;

import cn.hutool.core.bean.BeanUtil;
import com.kaigejava.demo.business.barrgelog.dto.DanMuMsgDTO;
import com.kaigejava.demo.business.barrgelog.service.IBarrageLogService;
import com.kaigejava.demo.business.enums.TypeEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import tech.ordinaryroad.live.chat.client.bilibili.msg.DanmuMsgMsg;
import tech.ordinaryroad.live.chat.client.commons.base.msg.ICmdMsg;
import tech.ordinaryroad.live.chat.client.commons.base.msg.IMsg;
import tech.ordinaryroad.live.chat.client.huya.client.HuyaLiveChatClient;
import tech.ordinaryroad.live.chat.client.huya.config.HuyaLiveChatClientConfig;
import tech.ordinaryroad.live.chat.client.huya.constant.HuyaCmdEnum;
import tech.ordinaryroad.live.chat.client.huya.listener.IHuyaMsgListener;
import tech.ordinaryroad.live.chat.client.huya.msg.MessageNoticeMsg;
import tech.ordinaryroad.live.chat.client.huya.msg.SendItemSubBroadcastPacketMsg;
import tech.ordinaryroad.live.chat.client.huya.netty.handler.HuyaBinaryFrameHandler;

import javax.annotation.Resource;

/**
 * @BelongsProject: douyin-live-java
 * @Author: kaigejava
 * @CreateTime: 2023-11-09  17:43
 * @Description: 虎牙直播
 * @Version: 1.0
 */
@Service
@Slf4j
public class HuYaGrabService extends BaseSendMessageService implements IGrabService{
    static Object lock = new Object();
    HuyaLiveChatClient client;
    @Resource
    private IBarrageLogService barrageLogService;
    @Override
    public void grab(String roomId,long timestamp) throws Exception {
        HuyaLiveChatClientConfig config = HuyaLiveChatClientConfig.builder()
                .roomId(Long.valueOf(roomId))
                /* .roomId(390001)
                 .roomId(527988)
                 .roomId(1995)
                 .roomId(116)
                 // bagea
                 .roomId(189201)*/
                .build();

        client = new HuyaLiveChatClient(config, new IHuyaMsgListener() {
            @Override
            public void onDanmuMsg(HuyaBinaryFrameHandler binaryFrameHandler, MessageNoticeMsg msg) {
                //log.info("{} 收到弹幕 {}({})：{}", binaryFrameHandler.getRoomId(), msg.getUsername(), msg.getUid(), msg.getContent());
                //入库保存
                DanMuMsgDTO danMuMsgDTO = DanMuMsgDTO.HuYaOf(msg,Long.parseLong(roomId));
                barrageLogService.save(danMuMsgDTO,TypeEnum.HY_STATION.getCode());
                //将弹幕发送到页面
                String message = "用户昵称【"+msg.getUsername()+"】"+",在【"+currentTime+"】的时候,发送的弹幕内容为:【"+msg.getContent()+"】";
                sendMessage(message, TypeEnum.HY_STATION.getCode()+"_"+roomId+"_"+timestamp);
            }

            @Override
            public void onGiftMsg(HuyaBinaryFrameHandler binaryFrameHandler, SendItemSubBroadcastPacketMsg msg) {
                long lPayTotal = msg.getLPayTotal();
                if (lPayTotal != 0) {
                    int giftPrice = msg.getGiftPrice();
                }
                // log.info("{} 收到礼物 {}({}) {} {}({})x{}({})", binaryFrameHandler.getRoomId(), msg.getUsername(), msg.getUid(), "赠送", msg.getGiftName(), msg.getGiftId(), msg.getGiftCount(), msg.getGiftPrice());
            }

            @Override
            public void onMsg(HuyaBinaryFrameHandler binaryFrameHandler, IMsg msg) {
                // log.debug("{} 收到{}消息 {}", binaryFrameHandler.getRoomId(), msg.getClass(), msg);
            }

            @Override
            public void onCmdMsg(HuyaBinaryFrameHandler binaryFrameHandler, HuyaCmdEnum cmd, ICmdMsg<HuyaCmdEnum> cmdMsg) {
                // log.info("{} 收到CMD消息{} {}", binaryFrameHandler.getRoomId(), cmd, cmdMsg);
            }

            @Override
            public void onOtherCmdMsg(HuyaBinaryFrameHandler binaryFrameHandler, HuyaCmdEnum cmd, ICmdMsg<HuyaCmdEnum> cmdMsg) {
                //log.debug("{} 收到其他CMD消息 {}", binaryFrameHandler.getRoomId(), cmd);
            }

            @Override
            public void onUnknownCmd(HuyaBinaryFrameHandler binaryFrameHandler, String cmdString, IMsg msg) {
                // log.debug("{} 收到未知CMD消息 {}", binaryFrameHandler.getRoomId(), cmdString);
            }
        });
        client.connect();

        // 防止测试时直接退出
        while (true) {
            synchronized (lock) {
                lock.wait();
            }
        }
    }

    @Override
    public void close(String roomId) throws Exception {
        client.disconnect();
        boolean isShutdown = client.getWorkerGroup().isShutdown();
        if(!isShutdown){
            client.getWorkerGroup().shutdownGracefully();
            System.out.println("关闭【虎牙】连接："+roomId);
        }
    }
}
